//
//  PhotoBrowserAnimator.swift
//  HMWeibo
//
//  Created by wulei on 15/12/20.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit

// MARK: - 展现动画协议
protocol PhotoBrowserPresentDelegate: NSObjectProtocol {
    // 返回用来做动画的 imageView
    func imageViewForPresent(indexPath: NSIndexPath) -> UIImageView
    // 返回转场的起始位置
    func photoBrowserPresonFromRect(indexPath: NSIndexPath) -> CGRect
    // 返回转场的目标位置
    func photoBrowserPresonToRect(indexPath: NSIndexPath) -> CGRect
}

// MARK: - 解除转场动画协议
protocol PhotoBrowserDismissDelegate: NSObjectProtocol {
    // 返回解除转场动画视图
    func imageViewForDismiss() -> UIImageView
    // 返回解除转场动画索引
    func indexPathForDismiss() -> NSIndexPath
}

class PhotoBrowserAnimator: NSObject,UIViewControllerTransitioningDelegate {
    // 展现动画的代理
    weak var presentDelegate: PhotoBrowserPresentDelegate?
    
    // 解除转场动画的代理
    weak var dismissDelegate: PhotoBrowserDismissDelegate?
    
    // 动画图像的索引
    var indexPath: NSIndexPath?
    
    
    
    // 是否modal 展现的标记
    private var isPresent = false
    
    // 设置代理相关参数
    func setDelegateParams(presentDelegate: PhotoBrowserPresentDelegate,
        indexPath: NSIndexPath,
        dismissDelegate: PhotoBrowserDismissDelegate) {
            
            self.presentDelegate = presentDelegate
            self.dismissDelegate = dismissDelegate
            self.indexPath = indexPath
    }
    
    // 指定要展现 modal 出来的动画 "对象" 的控制器
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    
    // 指定要 dismiss 的"动画对象"
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}


// MARK: - 实现具体的动画方法 (AnimatedTransitioning)
extension PhotoBrowserAnimator: UIViewControllerAnimatedTransitioning {
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    /// 实现具本的动画效果: 一旦使用了此方法,所有的动画代码交由程序员负责
    /// - parameter transitionContext: 转场动画的上下文 -提供动画所需要的素材
    
    /*
    1. containerView 容器视图: 会将 modal 出来的视图包装在容器视图中
    2. viewControllerForKey: fromVC / toVC
    3. viewForKey: fromV / toV
    4. completeTransition 无论转场是否取消都必须被调用
        否则系统不做其它事件处理!
    
    */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 一旦采用了自定义转场, 自动布局系统不会对根视图做任何约束
//        let v = UIView(frame: UIScreen.mainScreen().bounds)
//        v.backgroundColor = UIColor.redColor()
       let fromVC =  transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        
        print(fromVC)
        
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
        print(toVC)
        
        let fromV = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let toV = transitionContext.viewForKey(UITransitionContextToViewKey)
        print(fromV)
        print(toV)
        

        isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)

        
    }
    
    
    // 解除动画
   private func dismissAnimation(transitionContext: UIViewControllerContextTransitioning) {
    
    guard let dismissDelegate = dismissDelegate, presentDelegate = presentDelegate else {
        return
    }
    
    // 获取要dismiss 的控制器视图
        let fromV = transitionContext.viewForKey(UITransitionContextFromViewKey)!
    fromV.removeFromSuperview()
    
    // 获取图像视图
    let iv = dismissDelegate.imageViewForDismiss()
    // 添加到容器视图
    transitionContext.containerView()?.addSubview(iv)
    
    let indexPath = dismissDelegate.indexPathForDismiss()
    
    UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in

        iv.frame = presentDelegate.photoBrowserPresonFromRect(indexPath)
        
        }) { (_) -> Void in
            iv.removeFromSuperview()
            transitionContext.completeTransition(true)
    }
        
        
    }
    
    
    // 展现动画
   private func presentAnimation(transitionContext: UIViewControllerContextTransitioning) {
    guard let presentDelegate = presentDelegate, indexPath = indexPath else{
        return
    }
    
    // 目标视图
    let toV = transitionContext.viewForKey(UITransitionContextToViewKey)!
    transitionContext.containerView()?.addSubview(toV)
    
    // 图像视图
    let iv = presentDelegate.imageViewForPresent(indexPath)
    
    iv.frame = presentDelegate.photoBrowserPresonFromRect(indexPath)
    transitionContext.containerView()?.addSubview(iv)
    


    
    toV.alpha = 0
        
    UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
        
        iv.frame = presentDelegate.photoBrowserPresonToRect(indexPath)
        toV.alpha = 1
        
        }) { (_) -> Void in
            iv.removeFromSuperview()
            transitionContext.completeTransition(true)
    }
}
    
    
}
